package com.zatech.cgnci.project.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zatech.cgnci.project.base.context.UserContext;
import com.zatech.cgnci.project.base.enums.ResponseCodeEnum;
import com.zatech.cgnci.project.base.exception.GlobalException;
import com.zatech.cgnci.project.entity.CgnciOutstandingClaimsReserveDO;
import com.zatech.cgnci.project.entity.UserEntity;
import com.zatech.cgnci.project.mapper.CgnciOutstandingClaimsReserveMapper;
import com.zatech.cgnci.project.service.CgnciOutstandingClaimsReserveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import cn.hutool.poi.excel.ExcelReader;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 未决赔款准备金评估表 服务实现类
 * </p>
 *
 * @author Generator
 * @since 2025-01-20
 */
@Service
public class CgnciOutstandingClaimsReserveServiceImpl extends ServiceImpl<CgnciOutstandingClaimsReserveMapper, CgnciOutstandingClaimsReserveDO> implements CgnciOutstandingClaimsReserveService {

    @Override
    public void importClaimsReserve(MultipartFile file) {
        try (ExcelReader reader = ExcelUtil.getReader(file.getInputStream())) {
            List<Map<String, Object>> mapRead = reader.readAll();
            List<CgnciOutstandingClaimsReserveDO> claimsReserves = buildToProject(mapRead);
            if(CollectionUtils.isNotEmpty(claimsReserves)) {
                this.baseMapper.deleteAll();
                this.saveBatch(claimsReserves);
            }
        } catch (Exception e) {
            throw new GlobalException(ResponseCodeEnum.PARAMS_ILLEGAL, "导入模版有误"+e);
        }
    }

    @Override
    public ExcelWriter exportAll(Boolean derivedData) {
        List<CgnciOutstandingClaimsReserveDO> reserveDOS = new ArrayList<>();
        if(derivedData){
            reserveDOS =this.baseMapper.selectList(null);
        }
        ExcelWriter writer = ExcelUtil.getWriter(true);
        try {
            List<String> title = CollUtil.newArrayList("险种",
                    "险种代码",
                    "LR%赔付率",
                    "间接理赔费用率ULAE%",
                    "风险调整率RA%",
                    "赔款报案延迟天数");
            writer.writeHeadRow(title);
            for (CgnciOutstandingClaimsReserveDO reserveDO : reserveDOS) {
                writer.writeRow(Arrays.asList(
                        reserveDO.getRiskType(),
                        reserveDO.getRiskTypeCode(),
                        reserveDO.getLrLossRatio(),
                        reserveDO.getClaimsExpenseRatio(),
                        reserveDO.getRiskAdjustedRate(),
                        reserveDO.getDelayDays()
                ));
            }
        } catch (Exception e) {
            log.error("导出模版失败", e);
            throw new GlobalException(ResponseCodeEnum.PARAMS_ILLEGAL, "导出模版有误");
        }
        return writer;
    }

    @Override
    public List<CgnciOutstandingClaimsReserveDO> selectAll() {
        return this.baseMapper.selectList(null);
    }

    private List<CgnciOutstandingClaimsReserveDO> buildToProject(List<Map<String, Object>> inPut) {
        List<CgnciOutstandingClaimsReserveDO> claimsReserveDOS = new ArrayList<>();
        UserEntity user = UserContext.getUser();
        for (Map<String, Object> row : inPut) {
            CgnciOutstandingClaimsReserveDO claimsReserveDO = new CgnciOutstandingClaimsReserveDO();
            claimsReserveDO.setRiskType(String.valueOf(row.get("险种")));
            claimsReserveDO.setRiskTypeCode(String.valueOf(row.get("险种代码")));
            claimsReserveDO.setLrLossRatio(new BigDecimal(String.valueOf(row.get("LR%赔付率"))));
            claimsReserveDO.setClaimsExpenseRatio(new BigDecimal(String.valueOf(row.get("间接理赔费用率ULAE%"))));
            claimsReserveDO.setRiskAdjustedRate(new BigDecimal(String.valueOf(row.get("风险调整率RA%"))));
            claimsReserveDO.setDelayDays(Double.valueOf(String.valueOf(row.get("赔款报案延迟天数"))));
            claimsReserveDO.setCreateTime(new Date());
            claimsReserveDO.setCreator(user.getNameAndCode());
            claimsReserveDO.setIsDelete("N");
            claimsReserveDOS.add(claimsReserveDO);
        }
        return claimsReserveDOS;
    }
}
